package com.zts.modules.sys.controller;

import com.google.common.base.Strings;
import com.zts.base.BasicResourceAssembler;
import com.zts.base.page.PageBound;
import com.zts.base.resource.ListResource;
import com.zts.modules.sys.entity.Code;
import com.zts.modules.sys.entity.model.CodeModel;
import com.zts.modules.sys.entity.resource.CodeResource;
import com.zts.modules.sys.service.CodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.ExposesResourceFor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzk on 2017/6/16.
 */
@RestController("codeController")
@RequestMapping("/v1/codes")
@Api(value = "codes", description = "系统代码相关API")
@ExposesResourceFor(CodeResource.class)
public class CodeController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    CodeService codeService;
    private BasicResourceAssembler<Code, CodeResource> codeAssembler =
            new BasicResourceAssembler<>(CodeController.class, CodeResource.class);

    /**
     * 根据id获取syscode信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "getCodeById", httpMethod = "GET", value = "根据主键id查询syscode")
    public final CodeResource getCode(@ApiParam(required = true, value = "主键id")
                                      @PathVariable("id") Long id) {
        Code code = codeService.get(id);
        return codeAssembler.toResource(code);
    }

    /**
     * 新增代码
     *
     * @param codeModel
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "", method = RequestMethod.POST,
            consumes = {MediaType.APPLICATION_JSON_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    //方法仅处理request Content-Type为“application/x-www-form-urlencoded”和“multipart/form-data” 类型的请求。
    //方法仅处理request请求中Accept头中包含了"application/json"的请求，同时返回的内容类型为application/json;
    @ApiOperation(notes = "addCode", httpMethod = "POST", value = "新增代码")
    public final ListResource<CodeResource> insertCode(@ApiParam(required = true, value = "代码信息，接收数据格式为json")
                                                       @RequestBody List<CodeModel> codeModel) {
        Code code = null;
        List<Code> codeResourceslist = new ArrayList<Code>();
        for (CodeModel cm : codeModel) {
            code = codeService.insert(cm.transEntity());
            codeResourceslist.add(code);
        }
        return codeAssembler.toListResource(codeResourceslist);
    }

    /**
     * 修改代码
     *
     * @param codeModel
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "", method = RequestMethod.PUT,
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE,
                    MediaType.MULTIPART_FORM_DATA_VALUE,
                    MediaType.APPLICATION_JSON_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "updateCode", httpMethod = "PUT", value = "根据catacode与subcode修改代码信息")
    public final int updateCode(@RequestBody CodeModel codeModel) {
        System.out.println("step 1");
        logger.debug("updateUser param：{}", codeModel.toString());
        return codeService.update(codeModel.transEntity());
    }

    /**
     * 修改代码
     *
     * @param codeModel
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT,
            consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE,
                    MediaType.MULTIPART_FORM_DATA_VALUE,
                    MediaType.APPLICATION_JSON_VALUE},
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "updateCodeByid", httpMethod = "PUT", value = "根据id修改代码信息")
    public final int updateCodeByid(@ApiParam(required = true, value = "主键id") @PathVariable("id") Long id,
                                    @RequestBody CodeModel codeModel) {
        System.out.println("step 1");
        codeModel.setId(id);
        logger.debug("updateUser param：{}", codeModel.toString());
        return codeService.updateCodeByid(codeModel.transEntity());
    }

    /**
     * 通过id删除代码
     *
     * @param id
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "deleteCode", httpMethod = "DELETE", value = "根据主键id删除代码")
    public final int deleteCode(@ApiParam(required = true, value = "主键id") @PathVariable("id") Long id) {
        Code code = new Code();
        code.setId(id);
        return codeService.delete(code);
    }


    /**
     * 通过catacode与subCode删除代码
     *
     * @param cataCode
     * @param subCode
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "", method = RequestMethod.DELETE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "deleteCodeBycataAndSub", httpMethod = "DELETE", value = "根据类别标识与子类别标识删除代码")
    public final int deleteCodeBycataAndSub(@ApiParam(required = true, value = "类别标识") @RequestParam("cataCode") String cataCode,
                                            @ApiParam(required = false, value = "子类别标识") @RequestParam("subCode") String subCode) {
        Code code = new Code();
        code.setCataCode(cataCode);
        if (!Strings.isNullOrEmpty(subCode)) {
            code.setSubCode(subCode);
        }

        return codeService.deleteCodeBycataAndSub(code);
    }
    /**
     * 查询全部用户
     * 支持传入的参数参与模糊查询。
     *
     * @param codeModel
     * @return
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "getUsers(no id card)", httpMethod = "GET", value = "获取代码分页信息")
    public final ListResource<CodeResource> getUsers(@ApiParam(required = false, value = "系统代码查询条件")
                                                     @ModelAttribute("codeModel") CodeModel codeModel,
                                                     PageBound pageBound) {
        List<Code> list = codeService.getList(codeModel.transEntity(), pageBound);
        return codeAssembler.toListResource(list);
    }
}
